package com.scs.application.modules.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.BeanUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.Supplier;
import com.scs.application.modules.base.entity.SupplierDistribution;
import com.scs.application.modules.base.request.SupplierDistributionQueryRequest;
import com.scs.application.modules.base.service.SupplierDistributionService;
import com.scs.application.modules.base.vo.SupplierDistributionVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 供应商授权配送 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/supplier_distributions")
public class SupplierDistributionController extends CrudController<SupplierDistribution,String, SupplierDistributionService> implements ImAndExAbleController<SupplierDistributionQueryRequest> {


    @GetMapping("/page")
    public PageResult page(SupplierDistributionQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(SupplierDistributionQueryRequest request) {
        List<SupplierDistribution> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/page_authorize")
    public PageResult pageAuthorize(SupplierDistributionQueryRequest request){
        IPage page = service.pageAuthorize(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/page_authorize_supplier")
    public PageResult pageAuthorizeSupplier(SupplierDistributionQueryRequest request){
        IPage page = service.pageAuthorizeSupplier(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @PutMapping("addSupplier")
    public CommonResult addSupplier(@RequestBody List<SupplierDistributionVO> arrSelect){
        Supplier supplier = UserUtils.getSupplier();
        arrSelect.forEach(item ->{
            item.setSupplierId(supplier.getId());
            item.setSupplierName(supplier.getName());
        });
        List sd = new ArrayList<SupplierDistribution>(arrSelect.size());
        BeanUtils.copyProperties(arrSelect, sd, SupplierDistribution.class);

        boolean b = service.saveBatch(sd);
        if(b){
            return CommonResult.ok("添加成功");
        }else{
            return CommonResult.ok("添加失败");
        }
    }

    @PostMapping("delSupplier")
    public CommonResult delSupplier(@RequestBody List<String> arrRemove ){

        boolean b = service.remove(Wrappers.<SupplierDistribution>query().eq("supplier_id", UserUtils.getSupplierId()).in("authorize_id", arrRemove));
        if(b){
            return CommonResult.ok("删除成功");
        }else{
            return CommonResult.ok("删除失败");
        }
    }



}

